<?php
include("include/config.inc.php");

$pageName = 'material';

if($_SESSION['s_userType'] == 'User')
{
   $authorization = "NoAccess";
   foreach($selUsermenuArr as $value)
   {
      if(in_array($pageName, $value))
      {
        $authorization ="Access";
        break;
      }
      else 
      {
        $authorization = "NoAccess";    
      }
   }
   if($authorization == "NoAccess")
   {
     header("location:index.php");
   }
}

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $i = 0;
  $materialId   	= isset($_REQUEST['materialId']) ? $_REQUEST['materialId'] : 0;
  $materialName 		= "";
  $isEdit 	= 0;
  $viewArr  = array();
  $editmaterialId   = 0;
	$editmaterialName = "";
    
  if(isset($_POST['insertBtn']))
  {
    if(isset($_POST['updatematerialId']) && $_POST['updatematerialId'] > 0)
    {
      $updateMarkoQuery = "UPDATE material
             								  SET materialName = '".$_POST['materialName']."'
	            								WHERE materialId   = ".$_POST['updatematerialId'];
      $updateMarkoQueryResult = mysql_query($updateMarkoQuery);
      if(!$updateMarkoQueryResult)
      die("Update Query Not Inserted : ".mysql_error());
      else
      header("Location:material.php");  
    }       
    else
    {
      $insertQuerymaterial = "INSERT INTO material (materialName)
                              VALUE('".$_POST['materialName']."')";
      $insertQuerymaterialResult = mysql_query($insertQuerymaterial);
      if(!$insertQuerymaterialResult)
      die("Insert Query Not Inserted : ".mysql_error());
      else
      {
        header("Location:material.php");
      }
    }
  }
  $msg              = "Record Not Found";
  $materialDataArray = array();
  $i = 0;

  $selectmaterial = "SELECT materialId,materialName
                   FROM material
                   ORDER BY materialName";
  $selectmaterialResult = mysql_query($selectmaterial);
  while($materialdRow = mysql_fetch_array($selectmaterialResult))
  {
	  $materialDataArray[$i]['materialId']     = $materialdRow['materialId'];
	  $materialDataArray[$i]['materialName']   = $materialdRow['materialName'];
	  if($materialdRow['materialId']== $materialId)
	{
	  $editmaterialId   = $materialdRow['materialId'];
		$editmaterialName = $materialdRow['materialName'];
	}
	  $i++;
  }
}
include("./bottom.php");
$smarty->assign("materialDataArray",$materialDataArray);
$smarty->assign("materialId",$materialId);
$smarty->assign("materialName",$materialName);
$smarty->assign("i",$i);
$smarty->assign("isEdit",$isEdit);
$smarty->assign("msg",$msg);
$smarty->assign("editmaterialId",$editmaterialId);
$smarty->assign("editmaterialName",$editmaterialName);
$smarty->display('material.tpl');
?>